//
//  YBNormalShadowInputView.m
//  GongXinDangJian
//
//  Created by apple on 17/12/21.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "YBNormalShadowInputView.h"

@implementation YBNormalShadowInputView

- (instancetype)initWithTitle:(NSString *)title
{
    self = [super initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], [UIScreen zj_screenHeight])];
    if (self) {
        [self ybInitMyViewWithTitle:title];
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
    }
    return self;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch * touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    if (!CGRectContainsPoint(CGRectMake(ItemWidth * 30, ([UIScreen zj_screenHeight] - ItemHeight * 330) / 2, [UIScreen zj_screenWith] - ItemWidth * 60, ItemHeight * 330), point)) {
        [self hiddenViewAndCleanInputText];
    }
}


-(void)ybInitMyViewWithTitle:(NSString *)title{
    
    UIView * contentView = [[UIView alloc]initWithFrame:CGRectMake(ItemWidth * 30, 0, [UIScreen zj_screenWith] - ItemWidth * 60, ItemHeight * 330)];
    contentView.center = self.center;
    contentView.backgroundColor = [UIColor whiteColor];
    contentView.clipsToBounds = YES;
    contentView.layer.cornerRadius = normalCornerRadius;
    [self addSubview:contentView];
    
    UILabel * titleLabel = [ToolClass creatLabelWithFrame:CGRectMake(ItemWidth * 30, ItemHeight * 20, [UIScreen zj_screenWith] - ItemWidth * 120, ItemHeight * 60) font:18 textColor:[UIColor themeColor] text:title];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [contentView addSubview:titleLabel];
    
    [contentView addSubview:self.inputTextField];
    
    UIButton * cannelButton = [ToolClass createButtonWithFrame:CGRectMake(([UIScreen zj_screenWith] * 0.4 - ItemWidth * 60) / 3 , ItemHeight * 220, [UIScreen zj_screenWith] * 0.3, ItemHeight * 80) text:@"取消" textColor:[UIColor whiteColor] imageName:@"" font:16];
    cannelButton.backgroundColor = [UIColor themeColor];
    cannelButton.clipsToBounds = YES;
    cannelButton.layer.cornerRadius = normalCornerRadius;
    [cannelButton addTarget:self action:@selector(option_cannelButton:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:cannelButton];
    
    UIButton * ensureButton = [ToolClass createButtonWithFrame:CGRectMake(([UIScreen zj_screenWith] * 0.4 - ItemWidth * 60) / 3 * 2 + [UIScreen zj_screenWith] * 0.3 , ItemHeight * 220, [UIScreen zj_screenWith] * 0.3, ItemHeight * 80) text:@"确定" textColor:[UIColor whiteColor] imageName:@"" font:16];
    ensureButton.backgroundColor = [UIColor themeColor];
    ensureButton.clipsToBounds = YES;
    ensureButton.layer.cornerRadius = normalCornerRadius;
    [ensureButton addTarget:self action:@selector(option_ensureButton:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:ensureButton];
}


-(void)option_cannelButton:(UIButton *)sender{
    [self hiddenViewAndCleanInputText];
}


-(void)option_ensureButton:(UIButton *)sender{
    if (self.inputTextField.text.length == 0) {
        [YJProgressHUD showSuccess:[ToolClass returnNoNullStringWithString:self.noticeString] inview:self];
        return;
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(ybNormalShadowInputViewDidClickWithName:)]) {
        [self.delegate ybNormalShadowInputViewDidClickWithName:self.inputTextField.text];
    }
    [self hiddenViewAndCleanInputText];
}


-(void)hiddenViewAndCleanInputText{
    self.inputTextField.text = @"";
    self.hidden = YES;
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.inputTextField resignFirstResponder];
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    
    if (toBeString.length >= self.maxLength && range.length!= 1){
        textField.text = [toBeString substringToIndex:self.maxLength];
        return NO;
    }
    return YES;
}


-(UITextField *)inputTextField
{
    if (!_inputTextField) {
        _inputTextField = [[UITextField alloc]initWithFrame:CGRectMake(-5, ItemHeight * 100, [UIScreen zj_screenWith] - ItemWidth * 60 + 10, ItemHeight * 80)];
        _inputTextField.textAlignment = NSTextAlignmentCenter;
        _inputTextField.textColor = [UIColor textBlackColor];
        _inputTextField.font = LYPFONT(16);
        _inputTextField.returnKeyType = UIReturnKeyDone;
        _inputTextField.borderStyle = UITextBorderStyleRoundedRect;
        UIView * tempView1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ItemWidth * 30, ItemHeight * 80)];
        UIView * tempView2 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ItemWidth * 30, ItemHeight * 80)];
        _inputTextField.leftView = tempView1;
        _inputTextField.rightView = tempView2;
        _inputTextField.leftViewMode = UITextFieldViewModeAlways;
        _inputTextField.rightViewMode = UITextFieldViewModeAlways;
        _inputTextField.delegate = self;
    }
    return _inputTextField;
}


@end
